package com.iexbuy.ihg.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.entity.UserProfile;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.ui.activity.SingleFragmentActivity;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeWedeFragment extends BaseFragment {

	@ViewInject(R.id.pullToRefreshLayout)
	private PullToRefreshLayout pullToRefreshLayout;
	@ViewInject(R.id.exit)
	private Button exit;
	@ViewInject(R.id.commentSummry)
	private TextView commentSummry;

	private View rootView;
	private UserProfile profile;
	private static boolean isFirst;
	private int pageNo = 0;
	private List<DealHistoryDetailObject> dealHistoryListObjects = new ArrayList<DealHistoryDetailObject>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_wede, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View view) {
		mBaseActivity.setTitle(mBaseActivity.getResources().getString(R.string.nav_wode));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setLeftButtonVisibility(View.GONE);
		mBaseActivity.setRightButton(R.drawable.mine_setting, new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeSettingFragment.class);
			}
		});
		mBaseActivity.setSlideRightBackEnable(false);
		pullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initMyProfileData();
				initUserHeadrImage();
				initCommentData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				pullToRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
			}
		});

		pullToRefreshLayout.autoRefresh();
	}

	/*public void onResume() {
		super.onResume();
	*//*	if (isFirst){
			initMyProfileData();
			initUserHeadrImage();
		}
		isFirst=true;*//*
	}*/

	private void initMyProfileData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_PROFILE, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						profile = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), UserProfile.class);

						initMyProfileView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {

						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initUserHeadrImage() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_USER_HEADER_IMG, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						String headerImgUrl = JsonUtils.getJsonSubStringByKey(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), "headerImgUrl");
						if (StringUtil.isNotEmpty(headerImgUrl)) {
							App.bitmapUtils.display(((ImageView) rootView.findViewById(R.id.icon)), headerImgUrl);
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}


	private void initMyProfileView() {
		App.sPreferences.setCashBalance(String.valueOf(profile.getCashBalance()));
		App.sPreferences.setCoinBalance(String.valueOf(profile.getCoinBalance()));
		((TextView) rootView.findViewById(R.id.userName)).setText(App.sPreferences.getName());
		((TextView) rootView.findViewById(R.id.userMobile)).setText(App.sPreferences.getUserName());
		if (profile != null) {
			((TextView) rootView.findViewById(R.id.orderSummry)).setText( profile.getUnpaidDealNumber() + "笔");
			//((TextView) rootView.findViewById(R.id.cashSummry)).setText("余额" + profile.getCashBalance() + "元");
//			((TextView) rootView.findViewById(R.id.coinBalance)).setText("金币" + profile.getCoinBalance() + "个");
		}
	}
	public void initCommentData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
//		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		//0 表示未评价  1 表示已经评价
		paramMap.put("type", "0");
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_DEALS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {

					}

					@Override
					public void onCancelled() {

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						DealHistoryDetailListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), DealHistoryDetailListObjectResponce.class);
						if (pageNo == 0) {
							dealHistoryListObjects.clear();
						}
						dealHistoryListObjects.addAll(rep.getData());
						initComment();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});

	}

	private void initComment() {
		List<DealHistoryDetailObject> completeHistoryListObjects = new ArrayList<DealHistoryDetailObject>();
		for(int i=0;i<dealHistoryListObjects.size();i++){
			if(dealHistoryListObjects.get(i).getStatus().equals("4")){
				completeHistoryListObjects.add(dealHistoryListObjects.get(i));
			}
		}
		commentSummry.setText(completeHistoryListObjects.size() + "");
	}

	/**
	 * 我的个人资料
	 * @param arg0
	 */
	@OnClick(R.id.myProfileLayout)
	public void myProfileLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeProfileFragment.class);
	}

	/**
	 * 我的订单
	 * @param arg0
	 */
	@OnClick(R.id.orderLayout)
	public void orderLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeOrdersFragment.class);
	}

	/**
	 * 待付款
	 * @param arg0
	 */
	@OnClick(R.id.obligationLayout)
	public void obligationLayoutClick(View arg0) {
		WeDeOrdersFragment.setStatus("1");
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeOrdersFragment.class);
	}

	/**
	 * 我的现金已经删除
	 * @param arg0
	 */
	/*@OnClick(R.id.cashLayout)
	public void cashLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashRecordsFragment.class);
	}*/
	/**
	 * 我的金币
	 * @param arg0
	 */
	@OnClick(R.id.coinLayout)
	public void coinLayoutClick(View arg0) {

		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCoinRecordsFragment.class);
	}

	/**
	 * 我的兑现
	 * @param arg0
	 */
	@OnClick(R.id.mianDanLayout)
	public void mianDanLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashRecordsFragment.class);
		/**
		 * 下面是免单详情的跳转页面
		 */
		//CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeMianDanRecordsFragment.class);
	}

	/**
	 * 我的0购
	 * @param arg0
	 */
	@OnClick(R.id.yunGuoLayout)
	public void yunGuoLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeYunGouRecordsFragment.class);
	}

	/**
	 * 我的评价
	 * @param arg0
	 */
	@OnClick(R.id.commentLayout)
	public void commentLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDePeddingCommentFragment.class);
	}

	/**
	 * 我的设置
	 * @param arg0
	 *//*
	@OnClick(R.id.settingLayout)
	public void settingLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeSettingFragment.class);
	}
*/
	/**
	 * 我的反馈
	 * @param arg0
	 */
	@OnClick(R.id.feebackLayout)
	public void feebackLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeResponceCommentFragment.class);
	}

	/**
	 * 我的地址
	 * @param arg0
	 */
	@OnClick(R.id.addressLayout)
	public void addressLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeAddresssFragment.class);
	}

	/**
	 * 我的问题
	 * @param arg0
	 */
	@OnClick(R.id.questionLayout)
	public void questionLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCommonQuestionsFragment.class);
	}

	/**
	 * 我的分享
	 * @param arg0
	 */
	@OnClick(R.id.shareLayout)
	public void shareLayoutClick(View arg0) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeShareFragment.class);
	}

	@OnClick(R.id.exit)
	public void onExitClick(View arg0) {
		App.sPreferences.setLoginIn(false);
		Intent intent = new Intent(mBaseActivity, SingleFragmentActivity.class);
		intent.putExtra(ExtraKeyConstant.KEY_FRAGMENT, LoginFragment.class);
		intent.putExtra(ExtraKeyConstant.KEY_FINISH_ACTIVITY, true);
		startActivity(intent);
		mBaseActivity.finish();
	}



}
